#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include"bmpfile.h"

int main(void)
{
	BMP pb;
	
	char *ofile = "bmptest.bmp";
	int size = bmp_load(&pb, ofile);
	printf("zong jia zai zi jie shu:%d\n",size);
	
	printf("\n");
	printf("width: %d\n",pb.width);
	printf("height: %d\n",pb.height);
	printf("stride-行字节数: %d\n",pb.stride);
	printf("cdepth-像素位数: %d\n",pb.cdepth);
	printf("\n");
	
//错误：无效表达式的使用无效?????   void*------pb.pdata
//	printf("%d",*(pb.pdata + (3*1+2*pb.stride)));
	
//hua hong xin
//	if((x^(2)+y^(2)-1)^(3)-x^(2)y^(3)==0) bmp_setpixel(&pb, x, y, 255, 0, 0);
	float x,y,t;
	int tempX = 0,tempY = 272;
	for(y = 1.5f; y > -1.5f; y -= 0.1f) 
	{
		for(x = -1.5f; x < 1.5f; x += 0.05f) 
		{
			float a = x * x + y * y - 1;
//			putchar(a * a * a - x * x * y * y * y <= 0.0f ? '*' : ' ');
			if(a * a * a - x * x * y * y * y <= 0.0f)
			{
				bmp_setpixel(&pb, tempX, tempY, 255, 0, 0);
				tempX = tempX + 1;
			}
			else
			{
				tempX = tempX + 1;
				
			}
		}
		tempX = 0;
		tempY = tempY - 1;
	}
	
//jia sui ji zao dian
	int i,num = 0,x1,y1;
	num = rand() % (pb.width * pb.height / 55) + 55;
	for(i = 0;i < num ;i ++)
	{
		x1 = rand() % pb.width;
		y1 = rand() % pb.height;
		bmp_setpixel(&pb, x1, y1, 0, 255, 0);
	}

//hua ju xing kuang
	int tr,tg,tb,mr = 0,mg = 0,mb = 255,alpha = 120,hr,hg,hb;
	int w,h;
	for(w = 0;w < 55; w ++)
		for(h = 0;h < 55; h ++)
		{
			bmp_getpixel(&pb, 55 + w, 55 + h, &tr, &tg, &tb);
			hr = mr + alpha * (tr - mr) / 255;
			hg = mg + alpha * (tg - mg) / 255;
			hb = mb + alpha * (tb - mb) / 255;
			bmp_setpixel(&pb, 55 + w, 55 + h, hr, hg, hb);
		}

//jia dian zhen zi
	//由于处在的UTF-8编码环境，而去采用GB2312规范下HZK1616的字库处理方式，将导致字符乱码
	char *c = "How are you ?";
	int qm,wm,x2,y2,k,temp = 272,z,flag,aaa = 1;
	unsigned char buff[32];

	//对每个字节的八位进行位运算，得出其是否为0或非0，便是打点的位置
	unsigned char key[8] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

	FILE *hzk = fopen("HZK1616.dat","rb");

	while(*c)
	{
		qm = (uint8_t)c[0] - 0xA0;
		wm = (uint8_t)c[1] - 0xA0;
		fseek(hzk, (94 * (qm-1)  + (wm-1) ) * 32, SEEK_SET);
		fread(buff, 1, 32, hzk);

		for(x2 = 0;x2 < 16; x2 ++)
		{
			for(y2 = 0;y2 < 2; y2 ++)
				for(z=0; z<8; z++)
				{
					aaa++;
					flag = buff[x2*2+y2]&key[z];
					printf("%d",flag);
					if(flag != 0) bmp_setpixel(&pb, 174 + aaa , temp , 0, 0, 0);
				}
			temp--;
			aaa = 1;
		}

		c++;
	}
	fclose(hzk);





	int r,g,b;
	bmp_getpixel(&pb, 0, 0, &r, &g, &b);
	printf("54-----35----->36:x= 0 ,y= 0 , r->%d g->%d b->%d\n",r, g, b);
	printf("54-----35----->36:x= 0 ,y= 0 , r->%x g->%x b->%x\n",r, g, b);
	printf("\n");

	
	char *sfile = "bmpsave.bmp";
	int cize = bmp_save(&pb,sfile);
	printf("zong cun chu zi jie shu:%d\n",cize);
	
	bmp_free(&pb);
	
	
	return 0;
}
